/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaclientcp3110.UCM;
import common.Objects.Component;
import common.UCM.UCController;
import common.Objects.Product;
import javaclientcp3110.Model.MainModel;
import javaclientcp3110.Model.Product.ProductEditModel;
import javaclientcp3110.View.Product.ProductEditView;

/**
 * UC Edit Component
 *
 * Precondition: A valid user is logged into the system.
 *
 *  1. User selects to edit a product.
 *  2. System displays the edit product view.
 *  3. User selects a type of product.
 *  4. System loads a list of product of that type.
 *  5. User selects product from the list.
 *  6. System displays the details of that component.
 *  7. User edits the details and clicks save.
 *  8. System updates the product details in the storage medium.
 *  9. System displays a success message.
 *
 * Postcondition: The product details are updated in the system.
 *
 * Unsuccessful Outcomes:
 *
 *   * 3a) User enters invalid data.
 *        1. System displays an error message
 *        2. System prompts user to try again
 *   * 8a) System is unable to update the storage medium
 *        1. System displays an error message
 * @author Richard Irons
 */
public class UCEditProduct implements UCController{
    private ProductEditView view;
    private ProductEditModel model;
    /**
     * Defualt constructor, creates new model and view
     */
    public UCEditProduct() {
        this (new ProductEditView(), new ProductEditModel());
    }
    /**
     * Constructor that sets the model and view to the inputs
     * @param view - view to set to
     * @param model - model to set to
     */
    public UCEditProduct(ProductEditView view, ProductEditModel model) {
        this.view = view;
        this.model = model;
    }
    /**
     * Implement uccontrollers run function. This is the body of the use case, it
     * is where the use case is run.
     */
    public void run() {
    //*  1. User selects to edit a product.
    //*  2. System displays the edit product view.
    //*  3. User selects a type of product.
    //*  4. System loads a list of products of that type.
    //*  5. User selects product from the list.
    //*  6. System displays the details of that product.
    //*  7. User edits the details and clicks edit.
    //*  8. System updates the product details in the storage medium.

    //*   * 3a) User enters invalid data.
        if (validateProduct() == false) {
    //*        1. System displays an error message
    //*        2. System prompts user to try again
            view.errorMessage("The entered product details are not valid " +
                    "\n Please try again.");
        }
        else {
            Product tempProduct = new Product();
            if (view.getProdId()< 0){
                view.errorMessage("Nothing to edit");
            }
            else {
                tempProduct.setId(view.getProdId());
                tempProduct.setName(view.getProdName());
                tempProduct.setDescription(view.getProdDescription());
                tempProduct.setComponents(view.getProdComponents());
                tempProduct.setPrice(view.getProdPrice());

                model.setEditProduct(tempProduct);
                boolean itemEdited = model.editProduct();

                if (itemEdited) {
    //*  9. System displays a success message.
                    view.successMessage("Product successfully edited!");
                    MainModel.getInstance().updateData();
                }
                else {
    //*   * 8a) System is unable to update the storage medium
    //*        1. System displays an error message
                    view.errorMessage("Product could not be edited");
                }
            }
        }
    }
    private boolean validateProduct() {
        if (view.getProdName().equals(null)) {
            return false;
        }
        if (view.getProdDescription().equals(null)) {
            return false;
        }
        if (view.getProdComponents() == null || view.getProdComponents().isEmpty()) {
            return false;
        }
        try {
            view.getProdPrice();
        }
        catch (NumberFormatException e) {
            return false;
        }        
        return true;
    }
}
